package com.riamm.ui {
  import flash.events.MouseEvent;
  import com.riamm.interfaces.ILabelFor;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.text.TextField;
  /**
   * @author Saber
   */
  public class Label extends Component {
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function Label(content:String = "Label", format:TextFormat = null, xpos:int = 0, ypos:int = 0) {
      _text = content;
      _format = format;
      super(xpos, ypos);
    }
    //==========================================================================
    //  Properties
    //==========================================================================
    //----------------------------------
    //  autoSizeModel
    //----------------------------------
    private var _autoSizeModel:String = TextFieldAutoSize.NONE;
    public function get autoSizeModel():String {
      return _autoSizeModel;
    }
    public function set autoSizeModel(value:String):void {
      _autoSizeModel = value;
    }
    //----------------------------------
    //  text
    //----------------------------------
    private var _text:String;
    public function get text():String {
      return _text;
    }
    public function set text(value:String):void {
      _text = value;
      invalidata();
    }
    //----------------------------------
    //  format
    //----------------------------------
    private var _format:TextFormat;
    public function get format():TextFormat {
      return _format;
    }
    public function set format(value:TextFormat):void {
      _format = value;
      invalidata();
    }
    //==========================================================================
    //  Variables
    //==========================================================================
    private var textField:TextField;
    private var forComponent:ILabelFor;
    //==========================================================================
    //  Overridden methods
    //==========================================================================
    override protected function createFeature():void {
      textField = new TextField();
      _height = textField.height = 18;
      textField.selectable = false;
      textField.mouseEnabled = false;
      textField.text = _text;
      textField.autoSize = _autoSizeModel;
      addChild(textField);
    }
    override protected function draw():void {
      super.draw();
      textField.text = _text;
      textField.autoSize = _autoSizeModel;
    }
    //==========================================================================
    //  Public methods
    //==========================================================================
    public function labelFor(component:ILabelFor):void {
      if (component is ILabelFor) {
        if (this.hasEventListener(MouseEvent.CLICK)) {
          removeEventListener(MouseEvent.CLICK, clickHandler);
        }
        forComponent = component;
        addEventListener(MouseEvent.CLICK, clickHandler);
      }
    }
    //==========================================================================
    //  Event handlers
    //==========================================================================
    private function clickHandler(event:MouseEvent):void {
      if (forComponent) {
        forComponent.labelClickHandler();
      }
    }
  }
}
